package com.hyl.ssoclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    String ssoServerUrl;



    /**
     *无需登录就可以访问
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * 感知这次是在ssoserver登录成功跳回来的
     * @param model
     * @param session
     * @return
     */
    @GetMapping("/employess")
    public String emplorees(Model model, HttpSession session,@RequestParam(value = "token",required = false) String token){

        if (!StringUtils.isEmpty(token)){
            //1.TODO 去ssoserver获取当前token真正对应的用户信息
            session.setAttribute("loginUser","张三");
        }

        Object loginUser = session.getAttribute("loginUser");
        if (loginUser==null){
            //没登录，跳转到登录服务器进行登录
            return "redirect:"+ssoServerUrl+"?redirect_url=http://www.client1.com:8081/employess";
        }else {
            List<String> emps=new ArrayList<>();
            emps.add("小牛0");
            emps.add("张三");
            model.addAttribute("emps",emps);
            return "list";
        }

    }
}
